import { Controller, Get, Query } from '@nestjs/common';
import Redis from 'ioredis';

import { isEmpty } from 'lodash';
import * as svgCaptcha from 'svg-captcha';

import { InjectRedis } from '~/common/decorators/inject-redis.decorator';
import { genCaptchaImgKey } from '~/helper/genRedisKey';
import { generateUUID } from '~/utils/tool.util';

import { Public } from '../decorators/public.decorator';
import { ImageCaptcha } from '../models/auth.model';
import { ImageCaptchaDto } from '../dto/captcha.dto';

@Controller('auth/captcha')
export class CaptchaController {
  constructor(@InjectRedis() private redis: Redis) {}

  @Get('img')
  @Public()
  async captchaByImg(@Query() dto: ImageCaptchaDto): Promise<ImageCaptcha> {
    const { width, height } = dto;

    const svg = svgCaptcha.create({
      size: 4,
      color: true,
      noise: 4,
      width: isEmpty(width) ? 100 : width,
      height: isEmpty(height) ? 50 : height,
      charPreset: '1234567890'
    });
    const result = {
      img: `data:image/svg+xml;base64,${Buffer.from(svg.data).toString('base64')}`,
      id: generateUUID()
    };
    // 5分钟过期时间
    await this.redis.set(genCaptchaImgKey(result.id), svg.text, 'EX', 60 * 5);
    return result;
  }
}
